/**
 * @开发版权 
 * @项目名称 轻量数据中台(LDMP)
 * @版本信息 v1.0
 * @开发人员 zhous
 * @开发日期 2025-01-17
 * @修订日期
 * @描述  LogController
 */
package com.ng.sys.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.ng.common.annotation.TokenRequired;
import com.ng.common.controller.Controller;
import com.ng.common.model.response.R;
import com.ng.sys.model.domain.UserLog;
import jakarta.servlet.http.HttpServletRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@Tag(name = "日志管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/log")
public class LogController extends Controller {

	@Operation(summary = "分页获取日志列表")
	@Parameters({ @Parameter(name = "username", description = "用户名", required = false),
			@Parameter(name = "type", description = "日志类型(0-登录日志 1-系统日志)", required = false),
			@Parameter(name = "startTime", description = "开始时间", required = false),
			@Parameter(name = "endTime", description = "结束时间", required = false),
			@Parameter(name = "cur", description = "当前页码", required = true),
			@Parameter(name = "limit", description = "每页数据量", required = true) })
	@TokenRequired
	@GetMapping(value = { "/pages" }, produces = "application/json; charset=utf-8")
	public R<Page<UserLog>> logPages(HttpServletRequest request,
			@RequestParam(name = "username", required = false, defaultValue = "") String username,
			@RequestParam(name = "type", required = false, defaultValue = "") String type,
			@RequestParam(name = "startTime", required = false, defaultValue = "") String startTime,
			@RequestParam(name = "endTime", required = false, defaultValue = "") String endTime,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {

		String sqlExceptSelect = " FROM " + UserLog.TABLE_NAME + " A  WHERE 1=1 ";
		if (!StrKit.isBlank(startTime) && !StrKit.isBlank(endTime)) {
			sqlExceptSelect += " AND ( a.CREATE_TIME BETWEEN '" + startTime + "' AND '" + endTime + "'  ) ";
		}
		if (!StrKit.isBlank(type)) {
			sqlExceptSelect += " AND A.TYPE='" + type + "' ";
		}
		if (!StrKit.isBlank(username)) {
			sqlExceptSelect += " AND A.CREATE_USER_NAME='" + username + "' ";
		}

		sqlExceptSelect += " ORDER BY  A.CREATE_TIME DESC ";
		return R.ok(UserLog.dao.paginate(cur, limit, "SELECT A.* ", sqlExceptSelect));
	}

}
